package com.ss.android.article.base.feature.detail2.c;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Pair;
import android.webkit.WebView;

import com.ss.android.account.model.BaseUser;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.app.e.TTAndroidObject;
import com.ss.android.article.base.feature.subscribe.model.EntryItem;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

public final class NewArticleDetailTTAndroidObject extends TTAndroidObject {
    private JSONObject paramsJO;

    public interface WebActionListener extends WebListener {
        void webviewContentResize(int newheight);

        void playVideo(String arg1, int arg2, int arg3, String arg4);

        void playNativeVideo(String vid, int arg2, int arg3, int arg4, int status, String callbackid);

        void onGetSeriesLinkPosition(int arg1);

        void showTitleBarPgcLayout(boolean arg1);
    }

    public interface WebListener {
        void onDomReady(WebView webView);

        void comment(String userName, String text, long commentId, int arg4);
    }

    public interface StatusListener extends WebListener {
        void adImageShow(int arg1);

        void slideShow(int arg1, int arg2, int arg3);

        void zoomStatus(boolean arg1);

        void slideDownload(String arg1);

        void toggleGalleryBars();

        void onRelatedShow();

        void onAdImageClick();

        void adImageLoadFinish();
    }

    public static abstract class AbsOnGetSeriesLinkPositionListener implements WebActionListener {

        public void onGetSeriesLinkPosition(int arg1) {
        }
    }

    private WebListener a;
    private WebActionListener l;
    private StatusListener m;
    private int n;
    private ShareBean mShareBean;

    public NewArticleDetailTTAndroidObject(AppData appData, Context context) {
        super(appData, context);
        this.n = 0;
        this.mShareBean = null;
    }

    public final void a(int arg5, long commentId) {
        String v2 = null;
        if (arg5 > 0 && arg5 == this.n && commentId > 0 && !android.support.design.a.isTextEmpty(v2)) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", 1);
                jsonObject.put("comment_id", commentId);
                this.sendCallbackMsg(null, jsonObject);
            } catch (Exception exception) {
            }
        }
    }

    public final void a(WebListener arg2) {
        if ((arg2 instanceof WebActionListener)) {
            this.l = (WebActionListener) arg2;
        }

        if ((arg2 instanceof StatusListener)) {
            this.m = (StatusListener) arg2;
        }

        this.a = arg2;
    }

    public final void handleUri(Uri uri) {
        if (uri != null) {
            String event = uri.getHost();
            if (!android.support.design.a.isTextEmpty(event)) {
                if ("log_event".equals(event)) {
                    String query = uri.getQuery();
                    if ((query.contains("slide_detail")) && (query.contains("related_show")) && this.m != null) {
                        this.m.onRelatedShow();
                    }
                }

                if ("domReady".equals(event)) {
                    if (this.a == null) {
                        return;
                    }
                    this.a.onDomReady(this.getWebView());
                    return;
                }
                super.handleUri(uri);
            }
        }
    }

    private void adImageShow(int arg2) {
        if (this.m != null) {
            this.m.adImageShow(arg2);
        }
    }

    public final void a(int arg9, int arg10, final BaseUser baseUser) {
        String v3;
        JSONObject v2_1;
        Pair<Long, String> v0;
        if (baseUser == null) {
            return;
        }

        if (baseUser.mUserId <= 0) {
            return;
        }

        Iterator v2 = this.mSubscribeQueue.iterator();
        do {
            if (!v2.hasNext()) {
                break;
            }
            v0 = (Pair<Long, String>) v2.next();
            if (v0.first.longValue() == baseUser.mUserId) {
                v2.remove();
                if (android.support.design.a.isTextEmpty(v0.second)) {

                } else {
                    try {
                        v2_1 = new JSONObject();
                        v3 = "code";
                        int v1;
                        if (arg9 == 0) {
                            v1 = 1;
                        } else if (arg9 != 1009) {
                            v1 = 0;
                        } else {
                            v1 = 1;
                        }
                        v2_1.put(v3, v1);
                        v2_1.put("id", baseUser.mUserId);
                        this.sendCallbackMsg(v0.second, v2_1);
                    } catch (Exception v0_1) {
                    }
                }
                break;
            }
        } while (true);


        if (arg9 == 0 || arg9 == 1009) {
            String v1_1 = "user_action";
            long v2_2 = baseUser.mUserId;
            int v0_2 = baseUser.isFollowing() ? 1 : 0;
            this.a(v1_1, v2_2, v0_2);
            if (baseUser.mMediaId > 0) {
                v1_1 = "pgc_action";
                v2_2 = baseUser.mMediaId;
                v0_2 = baseUser.isFollowing() ? 1 : 0;
                this.a(v1_1, v2_2, v0_2);
                new Thread(new Runnable() {
                    public final void run() {
                        try {
                            if (NewArticleDetailTTAndroidObject.this.mContextRef == null) {
                                return;
                            }

                            if (NewArticleDetailTTAndroidObject.this.mContextRef.get() == null) {
                                return;
                            }
                            DBHelper.getInstance(NewArticleDetailTTAndroidObject.this.mContextRef.get())
                                    .a(EntryItem.obtain(baseUser.mMediaId), baseUser.isFollowing());
                        } catch (Exception exception) {
                        }
                    }
                }).start();
            }
            baseUser.isFollowing();
        }
    }

    public final void addProtectedFeature(List<String> protectFeatureList) {
        super.addProtectedFeature(protectFeatureList);
        protectFeatureList.add("systemShare");
    }

    public final boolean a(Context context) {
        return true;
    }

    public final boolean processJsMsg(JSMessage jsMessage, JSONObject jsonObject) throws Exception {
        String func = TextUtils.isEmpty(jsMessage.func) ? "" : jsMessage.func;
        int v2 = -1;
        String v1_1 = null;
        int v3_3 = 0;
        String text = null;
        long commentId;
        String userName = null;
        int v0_1;
        int v4 = 2;
        switch (func) {
            case "adImageClick": {
                if (this.m != null) {
                    this.m.onAdImageClick();
                    return false;
                } else {
                    return false;
                }
            }
            case "relatedShow": {
                this.adImageShow(3);
                return false;
            }
            case "monitor": {
                paramsJO = jsMessage.params;
                if (paramsJO != null) {
                    String service = paramsJO.optString("service");
                    int status = paramsJO.optInt("status");
                    paramsJO = paramsJO.optJSONObject("extra");
                    if (!TextUtils.isEmpty(service)) {
                        com.bytedance.article.common.a.g.serviceMonitor(service, status, paramsJO);
                    }
                }
                return true;
            }
            case "webviewContentResize":
                if (this.l != null) {
                    this.l.webviewContentResize(jsMessage.params.optInt("height"));
                }
                return true;
            case "monitor_performance": {
                JSONObject paramsJSONObject = jsMessage.params;
                if (paramsJSONObject != null) {
                    try {
                        userName = paramsJSONObject.optString("service");
                        commentId = paramsJSONObject.optLong("value");
                        JSONObject jo2 = new JSONObject();
                        paramsJSONObject = paramsJSONObject.optJSONObject("extra");
                        paramsJSONObject.put("duration", commentId);
                        if (TextUtils.isEmpty(((CharSequence) userName))) {
                            return true;
                        }
                        if (!jo2.has("value")) {
                            return true;
                        }
                        com.bytedance.article.common.a.g.monitorPerformance(userName, jo2, paramsJSONObject);
                    } catch (Exception exception) {
                    }
                }
                return true;
            }
            case "playNativeVideo":
                paramsJO = jsMessage.params;
                String callbackId = jsMessage.callbackId;
                if (paramsJO != null) {
                    int status;
                    String vid = null;
                    int sp = 0; 
                    try {
                        vid = paramsJO.optString("vid");
                        sp = paramsJO.optInt("sp");
                        JSONArray frameArray = paramsJO.optJSONArray("frame");
                        v3_3 = frameArray.optInt(2);
                        status = 3;
                        v4 = frameArray.optInt(status);
                        status = paramsJO.optInt("status");
                    } catch (Exception v0_4) {
                        v4 = 0;
                        status = 0;
                    }
                    this.l.playNativeVideo(vid, sp, v3_3, v4, status, callbackId);
                    if (this.l == null) {
                        return false;
                    }
                }
                return false;
            case "playVideo": {
                paramsJO = jsMessage.params;
                text = jsMessage.callbackId;
                if (paramsJO == null) {
                    return false;
                }
                int v1 = 0;
                try {
                    userName = paramsJO.optString("url");
                    JSONArray v0_6 = paramsJO.optJSONArray("frame");
                    v1 = v0_6.optInt(2);
                    v4 = 3;
                    v0_1 = v0_6.optInt(v4);
                } catch (Exception v0_4) {
                    v0_1 = v1;
                    v1 = v0_1;
                    v0_1 = 0;
                }
                if (this.l == null) {
                    return false;
                }
                this.l.playVideo(userName, v1, v0_1, text);
                return false;
            }
            case "systemShare": {
                jsonObject.put("code", this.extractShareBean(jsMessage.params) ? 1 : 0);
                return true;
            }
            case "onGetSeriesLinkPosition": {
                if (this.l != null) {
                    this.l.onGetSeriesLinkPosition(jsMessage.params.optInt("value"));
                }
                return true;
            }
            case "showTitleBarPgcLayout": {
                if (this.l != null) {
                    boolean show = jsMessage.params != null && jsMessage.params.optBoolean("show");
                    this.l.showTitleBarPgcLayout(show);
                }
                return true;
            }
            case "slideDownload": {
                if (jsMessage.params == null) {
                    return false;
                }
                String image_url = jsMessage.params.optString("image_url");
                if (android.support.design.a.isTextEmpty(image_url)) {
                    return false;
                }
                Context context = this.mContextRef != null ? this.mContextRef.get() : null;
                if (context == null) {
                    return false;
                }

                if (android.support.design.a.isTextEmpty(image_url)) {
                    return false;
                }
                if (this.m == null) {
                    return false;
                }
                this.m.slideDownload(image_url);
                return false;
            }
            case "slideShow": {
                paramsJO = jsMessage.params;
                if (paramsJO == null) {
                    return false;
                }
                paramsJO.optString("image_url");
                v2 = paramsJO.optInt("cur_index");
                if (v2 > 0) {
                    this.adImageShow(1);
                } else if (v2 == 0) {
                    this.adImageShow(0);
                }
                int v1 = paramsJO.optInt("all_pic");
                v0_1 = paramsJO.optInt("text_offset_top");
                if (this.m == null) {
                    return false;
                }

                this.m.slideShow(v1, v2, v0_1);
                return false;
            }
            case "zoomStatus": {
                paramsJO = jsMessage.params;
                if (paramsJO == null) {
                    return false;
                }
                if (this.m == null) {
                    return false;
                }
                this.m.zoomStatus("zoomIn".equals(paramsJO.optString("value")));
                return false;
            }
            case "toggleGalleryBars": {
                if (this.m == null) {
                    return false;
                }
                this.m.toggleGalleryBars();
                return false;
            }
            case "adImageLoadFinish": {
                if (this.m == null) {
                    return false;
                }
                this.m.adImageLoadFinish();
                return false;
            }
            case "adImageShow": {
                this.adImageShow(v4);
                return false;
            }
            case "comment": {
                paramsJO = jsMessage.params;
                ++this.n;
                commentId = 0;
                try {
                    userName = paramsJO.optString("user_name");
                    text = paramsJO.optString("text");
                    commentId = TTAndroidObject.optLong(paramsJO, "comment_id");
                } catch (Exception v0_4) {
                }
                if (this.a == null) {
                    return false;
                }
                this.a.comment(userName, text, commentId, this.n);
                return false;
            }
            default: {
                v0_1 = -1;
                break;
            }
        }
        return super.processJsMsg(jsMessage, jsonObject);

    }

    private boolean extractShareBean(JSONObject jsonObject) {
        if (jsonObject != null) {
            ShareBean shareBean = new ShareBean();
            try {
                shareBean.fromJSONObject(jsonObject);
                if (android.support.design.a.isTextEmpty(shareBean.platform)) {
                    return false;
                }
                if (android.support.design.a.isTextEmpty(shareBean.url)) {
                    return false;
                }
                this.mShareBean = shareBean;
                return true;
            } catch (Exception exception) {
            }
        }
        return false;
    }

    public final void addPublicFeature(List<String> publicFeatureList) {
        super.addPublicFeature(publicFeatureList);
        publicFeatureList.add("comment");
        publicFeatureList.add("playVideo");
        publicFeatureList.add("zoomStatus");
    }

    public final boolean b(String arg4) {
        boolean v0 = false;
        if (this.mShareBean != null) {
            if (!android.support.design.a.isTextEmpty(mShareBean.platform) && !mShareBean.platform.equals(arg4)) {
                return v0;
            }
            v0 = this.a(arg4, mShareBean);
        }

        return v0;
    }
}

